﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ALS.Communication.Contract.Enums;
using ALS.Component.Tools.AsyncTcp;
using ALS.Component.Tools.Helpers;

namespace ALS.Communication.Domain.Adapters
{
    /// <summary>
    /// 适配器工厂
    /// </summary>
    public class AdapterFactory
    {
        private const string adapterNameSpace = "ALS.Communication.Domain.Adapters.{0}Adapter";
        private static readonly Dictionary<string, IAdapter> adapterDictionary;

        static AdapterFactory()
        {
            adapterDictionary = new Dictionary<string, IAdapter>();
        }

        /// <summary>
        /// 创建适配器
        /// </summary>
        /// <returns></returns>
        public static IAdapter GetAdapter(CommendTypeEnum commendType)
        {
            var adapter = default(IAdapter);
            var adapterName = string.Format(adapterNameSpace, commendType.ToString());

            if (adapterDictionary.ContainsKey(adapterName))
            {
                adapter = adapterDictionary[adapterName];
            }
            else
            {
                adapter = (IAdapter)Activator.CreateInstance(Type.GetType(adapterName));
            }
            return adapter;
        }
    }
}
